package DyingBaby.demo;

import DyingBaby.backend.common.CacheConfig;
import DyingBaby.backend.common.InteractiveCacheConfig;
import DyingBaby.backend.exec.Executor;
import DyingBaby.backend.catalog.Catalog;
import DyingBaby.backend.tm.TransactionManager;
import DyingBaby.backend.tm.TransactionManagerImpl;
import DyingBaby.backend.dm.DataManager;
import DyingBaby.backend.dm.DataManagerImpl;
import DyingBaby.backend.vm.VersionManager;
import DyingBaby.backend.vm.VersionManagerImpl;
import java.io.File;

/**
 * SQL缓存命中演示程序
 * 展示如何在SQL操作中产生缓存命中
 */
public class SQLCacheHitDemo {
    
    public static void main(String[] args) {
        try {
            // 创建临时测试文件
            File testFile = File.createTempFile("sql_cache_demo", ".db");
            testFile.deleteOnExit();
            
            // 先删除可能存在的文件
            if (testFile.exists()) {
                testFile.delete();
            }
            
            System.out.println("=== SQL缓存命中演示程序 ===");
            
            // 初始化数据库组件
            Catalog catalog = new Catalog();
            Executor executor = new Executor(catalog);
            
            // 设置用户选择为查看日志
            InteractiveCacheConfig.setUserChoice(true);
            
            System.out.println("\n1. 创建测试表...");
            executor.execute("CREATE TABLE test_table (id INT, name VARCHAR(20))");
            
            System.out.println("\n2. 插入测试数据...");
            executor.execute("INSERT INTO test_table (id, name) VALUES (1, 'Alice')");
            executor.execute("INSERT INTO test_table (id, name) VALUES (2, 'Bob')");
            executor.execute("INSERT INTO test_table (id, name) VALUES (3, 'Charlie')");
            
            System.out.println("\n3. 测试自连接查询（应该产生缓存命中）:");
            System.out.println("执行: SELECT t1.id, t2.name FROM test_table t1, test_table t2 WHERE t1.id = t2.id");
            executor.execute("SELECT t1.id, t2.name FROM test_table t1, test_table t2 WHERE t1.id = t2.id");
            
            System.out.println("\n4. 测试子查询（应该产生缓存命中）:");
            System.out.println("执行: SELECT * FROM test_table WHERE id IN (SELECT id FROM test_table WHERE name = 'Alice')");
            executor.execute("SELECT * FROM test_table WHERE id IN (SELECT id FROM test_table WHERE name = 'Alice')");
            
            System.out.println("\n5. 测试聚合查询（应该产生缓存命中）:");
            System.out.println("执行: SELECT COUNT(*) FROM test_table");
            executor.execute("SELECT COUNT(*) FROM test_table");
            
            System.out.println("\n6. 测试更新操作（应该产生缓存命中）:");
            System.out.println("执行: UPDATE test_table SET name = 'Updated' WHERE id = 1");
            executor.execute("UPDATE test_table SET name = 'Updated' WHERE id = 1");
            
            System.out.println("\n7. 验证更新结果:");
            executor.execute("SELECT * FROM test_table WHERE id = 1");
            
            // 关闭资源
            // 注意：在实际应用中，这里应该关闭相关资源
            
            System.out.println("\n=== 演示完成 ===");
            System.out.println("观察日志中的'缓存命中'和'缓存未命中'消息！");
            System.out.println("注意：在复杂的SQL操作中，相同页面可能被多次访问，产生缓存命中。");
            
        } catch (Exception e) {
            System.err.println("演示失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
